﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;

using Perseus;

using Hetymine.Executioner.Commands;

namespace Hetymine.Executioner.Plugins.Commands.Lua {
    /// <summary>
    /// Interaction logic for LuaCommandConfigurator.xaml
    /// </summary>
    public partial class LuaCommandConfigurator : UserControl, ICommandConfigurator<ICommand> {
        private LuaCommand _Command;
        private bool _IsApply = false;
        private TextBox _ScriptBindingTextBox;

        public LuaCommandConfigurator() {
            InitializeComponent();

            this.cName.TextChanged += new TextChangedEventHandler(Name_TextChanged);
            this.cCallStrings.TextChanged += new TextChangedEventHandler(CallStrings_TextChanged);
                        
            // Because microsoft decided not to add a TextChanged event to the combobox, we bind the 
            // text property to a textbox object and use it's textchange event instead.
            this._ScriptBindingTextBox = new TextBox();
            Binding binding = new Binding();
            binding.Source = this.cScript;
            binding.Path = new PropertyPath("Text");
            this._ScriptBindingTextBox.SetBinding(TextBox.TextProperty, binding);
            this._ScriptBindingTextBox.TextChanged += new TextChangedEventHandler(Script_TextChanged);

            this.cParameters.TextChanged += new TextChangedEventHandler(Parameters_TextChanged);
            this.cDelimiter.TextChanged += new TextChangedEventHandler(Delimiter_TextChanged);
        }

        private void Name_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }
            
            this._Command.Name = this.cName.Text.Trim();

            this.OnCommandNameChanged(this, new EventArgs());
        }
        private void CallStrings_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            string[] calls = this.cCallStrings.Text.Split(
                ",",
                SplitOptions.Trim | SplitOptions.RemoveEmptyEntries
            );

            this._Command.CallStrings.Clear();
            this._Command.CallStrings.AddRange(calls);

            this.OnCommandNameChanged(this, new EventArgs());
        }

        private void Script_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.Script = this.cScript.Text.Trim();

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }
        private void Parameters_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.Parameters = this.cParameters.Text.Trim();

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }
        private void Delimiter_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.Delimiter = this.cDelimiter.Text.Trim();

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }

        #region ICommandConfigurator<ICommand> Members
        public void InitializeConfig(ICommand config) {
            this._Command = config as LuaCommand;

            this._IsApply = false;

            // Information
            this.cName.Text = this._Command.Name;
            this.cCallStrings.Text = String.Join(", ", this._Command.CallStrings.ToArray());

            this.cScript.Items.Clear();
            this.cScript.Text = this._Command.Script;            
            foreach (string cs in this._Command.Plugin.CallStrings) {
                // Dont add default call string
                if (cs != Executioner.Config.GetString("Lua", "Lua Call String", "lua")) {
                    ComboBoxItem cbi = new ComboBoxItem();
                    cbi.Content = cs + ".lua";
                    this.cScript.Items.Add(cs + ".lua");
                }
            }
            this.cParameters.Text = this._Command.Parameters;
            this.cDelimiter.Text = this._Command.Delimiter;

            this._IsApply = true;
        }
        public UserControl ConfigPanel { get { return this; } }

        public event EventHandler CommandNameChanged;
        protected virtual void OnCommandNameChanged(object sender, EventArgs e) {
            if (this.CommandNameChanged != null) {
                this.CommandNameChanged(sender, e);
            }
        }
        #endregion
    }
}
